package dao;

import entity.Page;
import entity.PersonalFile;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import util.DBUtil;

import java.awt.print.Book;
import java.sql.SQLException;
import java.util.List;

/**
 * @author 孙亚梅
 * @version V1.0
 * @Project javase2203
 * @Package dao
 * @Description:
 * @Date 2022/5/27 12:57
 */
public class PersonalDaoImpl implements Personaldao {

    private QueryRunner runner= new QueryRunner(DBUtil.getDateSource());


    @Override
    public List<PersonalFile> selectAll() {
        String sql="select * from profile;";
        try {
            return runner.query(sql, new BeanListHandler<>(PersonalFile.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public List<PersonalFile> selectAllByPage(Page page) {
        String sql="select * from user limit ?,?;";
        try {
            List<PersonalFile> list = runner.query(sql, new BeanListHandler<PersonalFile>(PersonalFile.class), page.getStartRows(),page.getPageSize());
//            List<PersonalFile> list = runner.query(sql, new BeanListHandler<PersonalFile>(PersonalFile.class), page.getStartRows(), page.getPageSize());
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int selectAllByPageCount() {
        String sql ="select count(*) from profile;";
        try {
            long count = runner.query(sql, new ScalarHandler<Long>());
            return  (int)count;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return 0;
    }

    @Override
    public int addOne(PersonalFile personalFile) {
        String sql ="insert into profile values(null,?,?,?,?,?,?,?)";
        try {
            return  runner.update(sql, personalFile.getName(), personalFile.getBirthday(), personalFile.getGender(), personalFile.getCareer(), personalFile.getAddress(), personalFile.getMobile(), personalFile.getPicture());
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return 0;
    }

    @Override
    public int deleteOne(String personalFileId) {
        String sql ="delete from profile where id=?";
        try {
            return  runner.update(sql,personalFileId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public PersonalFile selectOne(String personalFileId) {
        String sql = "select * from profile where id = ? ";
        try {
            return runner.query(sql, new BeanHandler<>(PersonalFile.class), personalFileId);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public int updateOne(PersonalFile personalFile) {
        String sql = "update profile set name = ?, birthday=? ,gender=?, career=?, address=? ,mobile=?,picture=?,where id = ? ";
        try {
            return  runner.update(sql, personalFile.getName(), personalFile.getBirthday(), personalFile.getGender(), personalFile.getCareer(), personalFile.getAddress(), personalFile.getMobile(), personalFile.getPicture(), personalFile.getId());

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return 0;
    }
}
